/*
 * dtnhttpsqs.cpp
 *
 *  Created on: Nov 19, 2011
 *      Author: hao
 */

#include "dtnhttpsqs.h"
#include "httpsqsclient.h"
#include "HandleRequest.h"
#include "QueueManager.h"
#include <iostream>
#include <string>
using namespace std;


#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#include <unistd.h>


#include <sys/types.h>  /* Primitive System Data Types */
#include <errno.h>      /* Errors */
#include <sys/wait.h>   /* Wait for Process Termination */
#include <stdlib.h>     /* General Utilities */
#include "malloc.h"
#include <vector>

int main(int argc, char** argv)
{
	pid_t childpid; /* variable to store the child's pid */
	int retval; /* child process: user-provided return code */
	//int status; /* parent process: child's exit status */
//	QueueManager* queueManager = new QueueManager();
//	vector<string> requestQueue = queueManager->getResquestQueue();
	childpid = fork();


	if (childpid >= 0) /* fork succeeded */
	{
		if (childpid == 0) /* fork() returns 0 to the child process */
		{

			char* msg;
			while(1){
				msg = httpsqs_get("text_java_request");
				if(msg[0]!='\0'){
					printf("%s\n", msg);
					processTextRequest(msg);
				}else{
					sleep(1);
				}
				free(msg);
			}
/*			string msg;
			
			while (1) {
				bool isBusy = false;
				for (int i = 0;i < requestQueue.size(); i++)
				{
					string currentQueue = requestQueue[i];
					msg = httpsqs_get(const_cast<char*>(currentQueue.c_str()));
					if (!msg.empty()) {
						if(currentQueue.substr(0,3)=="xml")
						{
							processXMLRequest(msg);
						}else if (currentQueue.substr(0,4)=="text")
						{
							cout << "text cmd" << msg << endl;
							processTextRequest(msg);
							cout << "ENd text cmd" << endl;
						}
						if(!isBusy)
						{
							isBusy=true;
						}
					}

				}
				if(!isBusy){
					sleep(1);
					//cout << "No busy" << endl;
				}
					

			}*/
			exit(retval); /* child exits with user-provided return code */
		}else /* fork() returns new pid to the parent process */
		{
			exit(0); /* parent exits */
		}
	} else /* fork returns -1 on failure */
	{
		perror("fork"); /* display error message */
		exit(0);
	}

}


